# Copyright (C) 2001-2003 The Written Word, Inc.
# Copyright (C) 2000-2001 The Written Word, LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# $Id: myio.py 236 2006-09-19 22:19:40Z china $

import os
import sys

maxpos = 72				# max position in line
maxmaxpos = 78				# max position if text can fit
					# on line
indent_space = ' ' * 10

# verbosity levels
VERBOSE_DATABASE = 1			# reading build database entries
VERBOSE_DEPOT = 2			# print depots being added

class MyIO:
  def __init__ (self, verbose = 0):
    self.last_stream = None
    self.pos = 0			# current position in line
    self.verbose = verbose
    self.first_text = 1

  def stdout (self, indent, prefix, msg, verbose_level = 0):
    if self.verbose < verbose_level:
      return

    self.write (sys.stdout, indent, prefix, msg)
    self.last_stream = sys.stdout

  def stderr (self, indent, prefix, msg, verbose_level = 0):
    if self.verbose < verbose_level:
      return

    self.write (sys.stderr, indent, prefix, msg)
    self.last_stream = sys.stderr

  def write (self, stream, indent, prefix, msg):
    if self.last_stream == sys.stderr and stream == sys.stderr and \
    self.pos == 0:
      stream.write (prefix + ':\n')

    # append ': ' to prefix if it exists
    if prefix:
      prefix = prefix + ': '

    while msg:
      space_pos = msg.find (' ')
      if space_pos == -1:
        text = msg
        msg = ''
      else:
        text = msg[:space_pos + 1]
        msg = msg[space_pos + 1:]

      if self.pos == 0:
        stream.write (indent_space[:indent] + prefix)
        self.pos = len (indent_space[:indent]) + len (prefix)
      self.pos += len (text)
      if self.pos > maxpos:
        if text[-1] == '\n':
          if self.pos <= maxmaxpos:
            stream.write (text)
            self.first_text = 1
            self.pos = 0
            continue

        if not self.first_text:
          stream.write ('\n')
          stream.write (indent_space[:indent])
        stream.write (prefix + text)
        self.pos = len (indent_space[:indent]) + len (prefix) + len (text)
      else:
        if self.first_text:
          stream.write (text)
          self.first_text = 0
        else:
          stream.write (text)

      # if last bit of text ended in newline, reset position in line
      # to start at beginning. else, more output to come but concatenate
      # to existing output by continuing on the same line.
      if text[-1] == '\n':
        self.first_text = 1
        self.pos = 0

    stream.flush ()
